package com.lxp.servicebase.exceptionHandler;

import com.lxp.servicebase.exception.MyException;
import com.lxp.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//统一异常处理类
@ControllerAdvice
@Slf4j
public class GlobalException {
    //处理全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        log.error(e.getMessage());
        e.printStackTrace();
        return R.error().message("全局异常统一处理...");
    }

    //处理特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e){
        log.error("错误错误错误");
        e.printStackTrace();
        return R.error().message("执行了ArithmeticException异常...");
    }

    //处理特定异常
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public R error(NullPointerException e){
        log.error(e.getMessage());
        e.printStackTrace();
        return R.error().message("执行了NullPointerException异常...");
    }

    //处理特定异常
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public R error(MyException e){
        //记录错误信息
        log.error(e.getMessage());
        e.printStackTrace();
        return R.error().code(e.getCode()).message(e.getMessage());
    }
}
